package com.soc.erp.operRecord;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.nature.framework.annotation.Service;
import org.nature.framework.auth.NatureAuther;
import org.nature.framework.cache.NatureContext;
import org.nature.framework.core.NatureCtrl;
import org.nature.framework.core.NatureService;
import org.nature.framework.util.StringUtil;

@Service
public class OperRecordService extends NatureService {

	public int insertOper(String content){
		HttpServletRequest request =NatureContext.getRequest();
		//System.out.println(request.getHeader("X-Forwarded-For"));
		String ip = request.getHeader("X-Forwarded-For");
        if(StringUtil.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = ip.indexOf(",");
            if(index != -1){
                ip = ip.substring(0,index);
            }
        }else{
        	ip = request.getHeader("X-Real-IP");
        	if(StringUtil.isBank(ip) || "unKnown".equalsIgnoreCase(ip) || "null".equals(ip)){
                ip = request.getRemoteAddr();
        	}
        }
        
        OperRecord operRecord = new OperRecord();
        operRecord.put(OperRecord.ip, ip);	
        
        //userid 有时候是系统用户或 有时候是公司用户
        if (StringUtil.isNotBank((String) request.getSession().getAttribute("systemuser_id"))) {
        	operRecord.put(OperRecord.userid,request.getSession().getAttribute("systemuser_id"));
        }
        else  if (StringUtil.isNotBank((String) request.getSession().getAttribute("company_id"))) {
        	operRecord.put(OperRecord.userid,request.getSession().getAttribute("company_id"));
        }
        	 
     
        
        operRecord.put(OperRecord.role_type, request.getSession().getAttribute("role_type"));
        operRecord.put(OperRecord.content, content);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        operRecord.put(OperRecord.oper_time, df.format(new Date()));
        return this.insert(operRecord);
	}
}
